<template>
  <div class="view-login">
    <el-form :model="form" ref="ruleForm" :rules="rules" label-width="60px">
      <div class="form-header">登录</div>
      <div class="form-body">
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" autocomplete="off"></el-input>
        </el-form-item>
      </div>
      <div class="form-footer">
        <el-button type="default" @click="handleReset">重置</el-button>
        <el-button type="primary" @click="handleSubmit">提交</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { formValidate } from '@/utils'
export default {
  data() {
    const rules = {
      username: [{ required: true, message: '不能为空' }],
      password: [{ required: true, message: '不能为空' }]
    }
    return {
      rules,
      form: {
        username: null,
        password: null
      }
    }
  },
  methods: {
    handleReset() {
      this.$refs.form.resetFields()
    },
    async handleSubmit() {
      const valid = await formValidate(this.$refs.ruleForm)
      if (!valid) return

      const success = await this.$store.dispatch('login', this.form)
      if (success) {
        this.$router.push('/')
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.view-login
  position absolute
  left 50%
  top 50%
  transform translate(-50%, -50%)
  width 420px
  border 1px solid #ccc
  border-radius 3px
  .form-header
    height 45px
    padding 0 20px
    line-height 45px
    background-color #efefef
  .form-body
    margin 40px
  .form-footer
    margin-right 40px
    margin-bottom 30px
    text-align right
</style>
